import address from '../common/address'
Page({
  openSetting() {

  },
  address: function () {   //弹起省市区选择器
    this.setData({
      show: true,
    })
  },
  cancel: function () { // 关闭省市区选择器
    this.setData({
      show: false
    })
  },
  conform: function (e) {  // 赋值选择器值
    // console.log(e.detail.values)
    var adr = e.detail.values
    var ad = Object.keys(adr)
    var s = ad.map((item) => {
      return adr[item].name
    })
    this.setData({
      show: false,
      value3: s.join(',')
    })
  },
  onClose: function () {  // 省市区选择器取消
    this.cancel()
  },
  tapDialogButton: function () {  // 校验弹框  确定
    this.setData({
      dialogShow: false
    })
    return false
  },
  detail: function (e) {  // 保存 校验
    var phone = this.data.value2
    if (this.data.value1 == '') {
      this.error(0)
      return false
    } else if (phone == null || phone == "" || !/^[1][3,4,5,7,8][0-9]{9}$/.test(phone)) {
      this.error(1)
      return false
    } else if (this.data.value4 == '') {
      this.error(3)
      return false
    } else if (this.data.value3 == '') {
      this.error(2)
      return false
    }
    var arr = wx.getStorageSync('addressList')
    if (arr) {
      this.setData({
        flag: false
      })
    } else {
      this.setData({
        flag: true
      })
      arr = []
    }
    if (this.data.isDeliver) {
      var str = wx.getStorageSync('addressList');
      str.map((item, indexs) => {
        if (this.data.DeliverObj.index === indexs) {
          item.name = this.data.value1,
            item.phone = this.data.value2,
            item.city = this.data.value3,
            item.home = this.data.value4
        }
      })
      wx.setStorageSync('addressList', str);
      wx.navigateBack({
        delta: 1,
      })
      return false
    }
    var obj = {
      name: this.data.value1,
      phone: this.data.value2,
      city: this.data.value3,
      home: this.data.value4,
      flag: this.data.flag,
    }
    arr.push(obj)
    wx.setStorageSync('addressList', arr);
    this.setData({
      value1: '',
      value2: '',
      value3: '',
      value4: ''
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  error: function (value) {
    this.setData({
      dialogShow: true
    })
    switch (value) {
      case 0:
        this.data.message = '收货人姓名不能为空';
        break;
      case 1:
        this.data.message = '手机号格式不符合要求';
        break;
      case 2:
        this.data.message = '地区不能为空';
        break;
      case 3:
        this.data.message = '地址不能为空';
        break;
      default:

    }
    this.setData({
      message: this.data.message
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    value1: '',
    value2: '',
    value3: '',
    value4: '',
    address: address,
    show: false,
    dialogShow: false,
    buttons: [{ text: '确定' }],
    message: '',
    flag: null,
    isDeliver: null,  // 这个属性是判断是不是编辑页传过来的
    DeliverObj: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('0000')
    this.setData({
      isDeliver:false
    })
    let eventChannel
    eventChannel = this.getOpenerEventChannel();
    eventChannel.on('para', (data) => {
      console.log('1111')
      this.setData({
        value1: data.name,
        value2: data.phone,
        value3: data.city,
        value4: data.home,
        isDeliver: true,
        DeliverObj: data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})